import { Command } from '../constant';
import { IMainReducer } from '../types';
import { Action } from 'typings';
import produce from 'immer';
import * as immerUtil from '@/redux/immer-util';

const INITIAL_STATE: IMainReducer = {
  isReady: false,
  isLoading: false,
  isClick: true,
  community: [],
  showCommunity: false,
  // 选中的小区名和id和地址
  communityId: '',
  communityName: '',
  communityAddress: '',
  // 以何种方式拉起地图
  whoMotherFuckerOpenTheMap: '',
  // 快递站点数据
  courierStation: [],
  showCourierStation: false,
  // 选中的快递站点
  courierStationId: '',
  courierStationName: '',
  // 送达时间
  showDeliveryTime: false,
  startTime: '',
  endTime: '',
  selectedTime: '',
  addressSelect: '',
  // 服务需求
  requirements: '',
  showRequirements: false,
  imgUrl: '',
  // 支付方式
  paymentMethod: 'wechat',
  points: {
    //用户拥有的总积分
    totalPoint: 0,
    //最多可使用的积分
    maxPoint: 0,
    //用戶使用的积分
    usePoint: 0,
    pointConfig: {},
    // 积分抵扣金额
    deductionAmountOfPoints: 0,
    // 积分抵扣开关
    integralDeductionSwitch: false
  },
  // 配送费
  deliveryFee: 0,
  // 生成的订单信息
  orderInfo: {},
  // 钱包支付相关
  // 用户手机号
  phone: '',
  // 余额
  accountBalanceTotal: 0,
  // 校验会员支付密码是否可用
  isPayPwdValid: false,
  // 设置支付密码弹窗1
  showSettingPassword: false,
  isForgetPassword: false,
  code: '',
  // 设置支付密码弹窗2
  showSettingPasswordNext: false,
  password: '',
  customerId: '',
  // 密码支付弹窗
  showPasswordPayment: false,
  // 密码输入错误
  passwordError: false,
  openid: '',
  // 查看明细弹窗
  showExpenseDetails: false,
  timeRaise: '--',
  basicServiceFee: '--',
  // 计价规则
  showRule: false,
  rule: { basicServicePoints: 0, durationFees: [] },
  // 积分抵扣规则弹窗
  showPointsRule: false,
  // 用户协议弹窗
  showUserAgreement: false,
  // 勾选用户协议
  userAgreement: false,
};

export default function main(state = INITIAL_STATE, action: Action): IMainReducer {
  const { type, payload } = action;

  return produce<IMainReducer>(state, (draftState) => {
    switch (type) {
      //通用改变数据
      case Command.commonChange:
        return immerUtil.commonChange(draftState, { ...payload, key: 'main' });

      //初始化
      case Command.init:
        draftState.isReady = true;
        for (let propKey in payload.main) {
          draftState[propKey] = payload.main[propKey];
        }
        return draftState;

      //重置
      case Command.clean:
        for (let propKey in INITIAL_STATE) {
          draftState[propKey] = INITIAL_STATE[propKey];
        }
        return draftState;
    }
  });
}
